package com.uni.crawl.service.impl.crawl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.uni.crawl.common.enums.crawl.CrawlPrefixEnum;
import com.uni.crawl.common.util.CodeUtil;
import com.uni.crawl.mapper.CrawlNamespaceMapper;
import com.uni.crawl.model.entity.crawl.CrawlNamespace;
import com.uni.crawl.model.form.crawl.CrawlNamespaceForm;
import com.uni.crawl.model.query.crawl.CrawlNamespacePageQuery;
import com.uni.crawl.model.vo.crawl.CrawlNamespaceNameVO;
import com.uni.crawl.model.vo.crawl.CrawlNamespaceVO;
import com.uni.crawl.service.PageQueryUtils;
import com.uni.crawl.service.crawl.CrawlNamespaceService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 爬虫空间 服务实现类
 * </p>
 *
 * @author JervisPG
 * @since 2023-12-16
 */
@Service
public class CrawlNamespaceServiceImpl extends MPJBaseServiceImpl<CrawlNamespaceMapper, CrawlNamespace> implements CrawlNamespaceService {

    @Override
    public Page<CrawlNamespaceVO> getPage(CrawlNamespacePageQuery pageQuery) {
        String nsCode = pageQuery.getNsCode();
        String nsName = pageQuery.getNsName();

        MPJLambdaWrapper<CrawlNamespace> wp = MPJWrappers.lambdaJoin();
        wp.eq(StrUtil.isNotEmpty(nsCode), CrawlNamespace::getNsCode, nsCode);
        wp.like(StrUtil.isNotEmpty(nsName), CrawlNamespace::getNsName, nsName);
        wp.eq(Objects.nonNull(pageQuery.getNsStatus()), CrawlNamespace::getNsStatus, pageQuery.getNsStatus());
        PageQueryUtils.wrapTimeRange(wp, CrawlNamespace::getCreateTime, pageQuery);

        return selectJoinListPage(PageQueryUtils.getPage(pageQuery), CrawlNamespaceVO.class, wp);
    }

    @Override
    public List<CrawlNamespaceNameVO> nameList() {
        MPJLambdaWrapper<CrawlNamespace> wp = MPJWrappers.lambdaJoin();
        wp.orderByDesc(CrawlNamespace::getId);
        return selectJoinList(CrawlNamespaceNameVO.class, wp);
    }

    @Override
    public boolean add(CrawlNamespaceForm form) {
        CrawlNamespace ns = BeanUtil.copyProperties(form, CrawlNamespace.class);
        ns.setNsCode(CodeUtil.genCode(CrawlPrefixEnum.NS));
        return save(ns);
    }

    @Override
    public boolean update(Long id, CrawlNamespaceForm form) {
        CrawlNamespace dbNs = getById(id);
        if (dbNs == null) {
            return false;
        }

        CrawlNamespace ns = BeanUtil.copyProperties(form, CrawlNamespace.class);
        ns.setId(dbNs.getId());
        return updateById(ns);
    }

    @Override
    public CrawlNamespaceForm getForm(Long id) {
        CrawlNamespace ns = getById(id);
        return BeanUtil.copyProperties(ns, CrawlNamespaceForm.class);
    }

    @Override
    public CrawlNamespace getByNsCode(String nsCode) {
        LambdaQueryWrapper<CrawlNamespace> wp = Wrappers.lambdaQuery();
        wp.eq(CrawlNamespace::getNsCode, nsCode);
        return getOne(wp);
    }

}
